package com.ghxxzx.daglcustom.business_form.web;

import com.ghxxzx.daglcustom.business_form.domain.dto.FormSubmitDto;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tmsps.ne4spring.base.BaseAction;
import org.tmsps.ne4spring.utils.ChkUtil;
import org.tmsps.ne4spring.utils.JsonUtil;
import org.tmsps.ne4spring.utils.wrapper.WrapMapper;
import org.tmsps.ne4spring.utils.wrapper.Wrapper;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 已废弃
 * 
 * 由 FormSubmitAction2 代替
 * 
 * @author 刘华
 *
 */
@Deprecated
@RestController
@RequestMapping("/form/workflow/Deprecated")
public class FormSubmitAction extends BaseAction {

	@Resource
	private TaskService taskService;

	@PostMapping("/submit")
	public Wrapper<String> submit(@Valid @RequestBody FormSubmitDto formSubmitDto) {
		String task_id = formSubmitDto.getTask_id();
		String form = formSubmitDto.getForm();

		Task task = taskService.createTaskQuery().taskId(task_id).singleResult();
		if (task == null) {
			return WrapMapper.ok("您已提交成功!");
		}

		logger.info("表单提交...");
		Map<String, Object> variables = taskService.getVariables(task.getId());
		JSONObject formJson = JsonUtil.jsonStrToJsonObject(form);
		logger.info("formJson--{}", formJson);
		Set<String> keySet = formJson.keySet();
		for (String key : keySet) {
			Object val = null;
			if (variables.containsKey(key)) {
				Object valObj = variables.get(key);
				if (valObj == null) {
					continue;
				}
				Class<?> valClass = valObj.getClass();
				logger.info("{}--数据类型class--{}", key, valClass);
				String valStr = formJson.getString(key);
				if (valClass == Integer.class || valClass == Long.class || valClass == int.class
						|| valClass == long.class) {
					val = ChkUtil.getInteger(valStr);
				} else {
					val = valStr;
				}

			} else {
				val = formJson.getString(key);
			}

			logger.info("{}:{}", key, val);
			taskService.setVariable(task.getId(), key, val);
			taskService.setVariableLocal(task.getId(), key, val);
		}
		logger.info("表单预设置完成!");

		List<String> userList = new ArrayList<>();
		userList.add("张三");
		userList.add("李四");
		userList.add("王五");
		taskService.setVariable(task.getId(), "userList", userList);

		taskService.setVariable(task.getId(), "cnt", 0);

		taskService.complete(task.getId());

		// 获取当前实例的 @super 任务 , 并将其替换为当前直属领导
		String superUserId = "abc";
		TaskQuery taskQuery = taskService.createTaskQuery().processInstanceId(task.getProcessDefinitionId())
				.taskAssignee("@super");
		if (taskQuery.count() > 0) {
			List<Task> list = taskQuery.list();
			for (Task task2 : list) {
				task2.setAssignee(superUserId);
				taskService.saveTask(task2);
			}
		}

		return WrapMapper.ok("提交成功!");
	}

}
